﻿
#pragma strict
#pragma implicit
#pragma downcast

public var owner : NetworkPlayer;

//The input values the server will execute on this object
private var serverCurrentCommandInput : int = 0;

/////////////////BEGIN////////////////////////
var thisName : String = "Bugged name";
var rigidBodyView : NetworkView;
var hp : int = 100;
//var theScoreBoard : scoreBoard;
var localPlayer : boolean = false;
//////////////////END////////////////////////

function Awake(){
	// We are probably not the owner of this object: disable this script.
	// RPC's and OnSerializeNetworkView will STILL get trough!
	// The server ALWAYS run this script though
	//if(Network.isClient){
	//	enabled=false;	 // disable this script (this enables Update());	
	//}	
	
	////////////////BEGIN////////////////////
	//orgMaterial = renderer.material;
	
	//theScoreBoard= GameObject.Find("Generalscripts").GetComponent(scoreBoard);
	/////////////////END///////////////////
}

function OnNetworkInstantiate (msg : NetworkMessageInfo) {
	// This is our own player
	if (networkView.isMine)
	{
		//camera.main.enabled=false;
		localPlayer=true;
		networkView.RPC("setName", RPCMode.Others, thisName);
		
		thisName=PlayerPrefs.GetString("playerName");
			
	}
	// This is just some remote controlled player, don't execute direct
	// user input on this. DO enable multiplayer controll
	else
	{
		thisName="Remote"+Random.Range(1,10);
		name += thisName;
	
		var tmp1 : Movement = GetComponent(Movement);
		tmp1.enabled = false;
		var tmp2 : Controls = GetComponent(Controls);
		tmp2.enabled = false;
		
		networkView.RPC("askName", networkView.viewID.owner, Network.player);

	}
}

function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
{
	if (stream.isWriting){
		//This is executed on the owner of the networkview
		//The owner sends it's position over the network
		
		var pos : Vector3 = transform.position;	
		var rot : Quaternion = transform.rotation;	
		stream.Serialize(pos);//"Encode" it, and send it
		stream.Serialize(rot);//"Encode" it, and send it
				
	}else{
		//Executed on all non-owners
		//This client receive a position and set the object to it
		
		var posReceive : Vector3 = Vector3.zero;
		var rotReceive : Quaternion = Quaternion.identity;
		stream.Serialize(rotReceive);
		stream.Serialize(posReceive); //"Decode" it and receive it
		
		
		//We've just recieved the current servers position of this object in 'posReceive'.
		
		transform.position = posReceive;
		transform.rotation = rotReceive;
		//To reduce laggy movement a bit you could comment the line above and use position lerping below instead:	
		transform.position = Vector3.Lerp(transform.position, posReceive, 0.9); //"lerp" to the posReceive by 90%
		transform.rotation = Quaternion.Lerp(transform.rotation, rotReceive, 0.9);
		
	}
}

@RPC
function SendMovementInput(CommandInput : int){	
	//Called on the server
	serverCurrentCommandInput = CommandInput;
}

@RPC
function SetPlayer(player : NetworkPlayer){
	owner = player;
	if(player==Network.player){
		//Hey thats us! We can control this player: enable this script (this enables Update());
		enabled=true;
	}
}

@RPC
function Respawn(){
	if (networkView.isMine)
	{
		//theScoreBoard.LocalPlayerDied();
			
		// Randomize starting location
		var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
		var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
	
		transform.position=spawnpoint.position;
		transform.rotation=spawnpoint.rotation;	
	}
	//hp=100;
}

@RPC
function setName(name : String){
	thisName=name;
}

@RPC
function askName(asker : NetworkPlayer){
	networkView.RPC("setName", asker, thisName);
}






